//发送验证码
Vue.component('sendcode', {
    props: ['phone','type','stamp'],
    template: '<a @click="sendCode" :style="[baseStyle,activeStyle]">{{sentTime}}{{tips}}</a>',
    data: function () {
        return {
            sent: false,
            hasClicked: false,
            sentTime: '',
            tips: '获取验证码',
            int: null,
            baseStyle: {
                position: 'absolute',
                width:'84px',
                height: '30px',
                marginTop: '-25px',
                textAlign:'center',
                lineHeight:'30px',
                color: '#F95656',
                borderRadius: '15px',
                border:'1px solid #F95656',
                fontSize: '12px',
            }
        }
    },
    computed: {
        activeStyle: function () {
            return {
                // backgroundColor: this.sent ? '#cccccc' : '#e5001e'
            }
        },
    },
    watch: {
        sentTime: function (n) {
            if (n > 0) {
                return;
            } else {
                this.sent = false;
                this.sentTime = '';
                this.tips = '发送验证码';
                clearInterval(this.int);
            }
        }
    },
    methods: {
        sendCode: function () {
            if (this.sent || this.hasClicked) {
                return;
            }

            var data = {
                phone: this.phone,
                picVerification: 1,
                picTimeStamp: 222111,
            };
            if(!(/^1(3|4|5|6|7|8|9)\d{9}$/.test(this.phone))){
							this.codeMessage("请输入正确的手机号");
							return false;
						}

            var str = pinet.formatData(data);
            var sign = pinet.md5(str);

            data['sign'] = sign;

            var that = this;
            var callback = function (ret) {
							if(this.type == 1){
								that.codeMessage(ret.message)
							}
              setPrefs('timeStamp',ret.content.timeStamp);

							that.hasClicked = false;
							that.sent = true;
							that.changeTips();
            };
            var errback = function (ret) {
							if(ret.errcode == -4003){
								that.codeMessage("请输入正确的手机号")
							}

            };
            pinet.post(APIURLP+'/play/register/getPhoneVerification', data, callback, errback);
        },
        changeTips: function () {
            this.sentTime = 60;
            this.tips = 's后重发';
            var that = this;
            this.int = setInterval(function () {
                that.sentTime--;
            }, 1000);
				},
				codeMessage:function(message){
					this.$emit('codemessage', message)
				}
    }
});
Vue.component('sendcodepd', {
    props: ['phone','type'],
    template: '<a @click="sendCode" :style="[baseStyle,activeStyle]">{{sentTime}}{{tips}}</a>',
    data: function () {
        return {
            sent: false,
            hasClicked: false,
            sentTime: '',
            tips: '获取验证码',
            int: null,
            baseStyle: {
                position: 'absolute',
                width:'84px',
                height: '30px',
                marginTop: '-25px',
                textAlign:'center',
                lineHeight:'30px',
                color: '#F95656',
                borderRadius: '15px',
                border:'1px solid #F95656',
                fontSize: '12px',
            }
        }
    },
    computed: {
        activeStyle: function () {
            return {
                // backgroundColor: this.sent ? '#cccccc' : '#e5001e'
            }
        },
    },
    watch: {
        sentTime: function (n) {
            if (n > 0) {
                return;
            } else {
                this.sent = false;
                this.sentTime = '';
                this.tips = '发送验证码';
                clearInterval(this.int);
            }
        }
    },
    methods: {
        sendCode: function () {
            if (this.sent || this.hasClicked) {
                return;
            }

            var data = {
                token: getPrefs('token'),
            };

            //1.加密
            // var str = '124中文内容';
            var base = new Base64();
            var base64 = base.encode(JSON.stringify(data));

            var that = this;
            var callback = function (ret) {
              console.log(JSON.stringify(ret));
							if(this.type == 1){
								that.codeMessage(ret.message)
							}
							that.hasClicked = false;
							that.sent = true;
							that.changeTips();
            };
            var errback = function (ret) {
              console.log(JSON.stringify(ret))
							if(ret.errcode == -4003){
								that.codeMessage("请输入正确的手机号")
							}
            };
            // var str = 'JTdCJTIydG9rZW4lMjIlM0ElMjJlZmFiMDZhYWUwNzE0MDlhODliMWM5ZmZhODQ1NmFkMCUyMiU3RA==';
            var obj = {
              jsonData:base64
            }
            pinet.post(APIURLP+'/play/cd/getCd', obj, callback, errback);
        },
        changeTips: function () {
            this.sentTime = 60;
            this.tips = 's后重发';
            var that = this;
            this.int = setInterval(function () {
                that.sentTime--;
            }, 1000);
				},
				codeMessage:function(message){
					this.$emit('codemessage', message)
				}
    }
});

//点击获取邀请码
Vue.component('getInvite_code', {
    template: "<div style='text-align:center;'><span @click='getInviteCode' style='padding:0 15px;border:1px solid #eee;border-radius:10px;height:30px;line-height:30px;background-color:#f09401;color:white;'>没有邀请码点击获取</span></div>",
    methods: {
        getInviteCode: function () {
            var that = this;
            var callback = function (ret) {
                that.$emit('send-invite_code', ret.content);
            };
            pinet.post(URL.getInviteCode, {}, callback);
        }
    },
});

Vue.component('lazy-load', {
    props: ['countPerPage', 'arr', 'url', 'param', 'retAttr'],
    template: "<div style='font-size:14px;text-align:center;margin-bottom:10px;'>{{hasMore ? '正在加载更多...' : '已到底部啦'}}</div>",
    data: function () {
        return {
            page: 1,
            busy: false,
            noMore: false,
        }
    },
    computed: {
        countOfLastPage: function () {
            return this.arr.length % this.countPerPage;

        },
        hasMore: function () {
            return this.countOfLastPage == 0 && !this.noMore;
        },
    },
    watch: {
        arr: {
            handler: function (n) {
                if (n.length <= this.countPerPage) {
                    this.page = 1
                }
            },
            deep: true,
        }
    },
    methods: {
        loadMore: function () {
            if (!this.hasMore) {
                return;
            }
            this.busy = true;

            var data = {
                page: this.page + 1,
                pageSize: this.countPerPage,
                member_id: getPrefs('member_id')
            };

            if (this.param) {
                concatJson(data, this.param);
            }

            var that = this;
            var callback = function (ret) {
                var more = that.retAttr ? ret.content[that.retAttr] : ret.content;
                if (more.length > 0) {
                    that.arr.push.apply(that.arr, more);
                    that.page++;
                } else {
                    that.noMore = true;
                }
                that.busy = false;
            };
            var errback = function () {
                that.busy = false
            };
            pinet.post(this.url, data, callback, errback);
        },
        doScroll: function () {
            if (document.body.scrollTop + window.innerHeight >= document.body.scrollHeight) {
                if (!this.busy) {
                    this.loadMore();
                }
            }
        },
    },
    mounted: function () {
        window.addEventListener('scroll', this.doScroll);
    }

});

Vue.component('alilazy-load', {
    props: ['countPerPage', 'arr', 'url', 'otherDatas', 'retAttr'],
    template: "<div style='font-size:14px;text-align:center;margin-bottom:10px;' id='loadmore'>{{hasMore ? '正在加载更多...' : '已到底部啦'}}</div>",
    data: function () {
        return {
            page: 1,
            busy: false,
            noMore: false,
        }
    },
    computed: {
        countOfLastPage: function () {
          if(this.arr.length==0){
            return 0 ;
          }else {
          return this.arr.length % this.countPerPage;
          }
        },
        hasMore: function () {
            return this.countOfLastPage == 0 && !this.noMore;
        },
    },
    watch: {
        arr: {
            handler: function (n) {
                if (n.length <= this.countPerPage) {
                    this.page = 1
                }
            },
            deep: true,
        }
    },
    methods: {
        loadMore: function () {
            if (!this.hasMore) {
                return;
            }
            this.busy = true;

            var data = {
                page: this.page + 1,
                pageSize: this.countPerPage,
                member_id: getPrefs('member_id')
            };

            if (this.otherDatas) {
                concatJson(data, this.otherDatas);
            }

            var that = this;
            pinet.get(this.url+'&toPage='+(this.page + 1)+'&perPageSize='+this.countPerPage, function (result) {
                var more = result.data.pageList;
                if (more.length > 0) {
                    for(var i=0;i<more.length;i++){
                      more[i].select = '-1';
                    }
                    that.arr.push.apply(that.arr, more);
                    that.page++;
                } else {
                    that.noMore = true;
                }
                that.busy = false;
            });
        },
        doScroll: function () {
            if (document.body.scrollTop + window.innerHeight >= document.body.scrollHeight) {
                if (!this.busy) {
                  // document.getElementById('search-container').style.display = 'none';
                    this.loadMore();
                }
            }
        },
    },
    mounted: function () {
        window.addEventListener('scroll', this.doScroll);
    }

});

Vue.component('limit-load', {
    props: ['countPerPage', 'arr', 'url', 'param', 'retAttr'],
    template: "<div style='font-size:14px;text-align:center;margin-bottom:10px;'>{{hasMore ? '正在加载更多...' : '已到底部啦'}}</div>",
    data: function () {
        return {
            limit_id: 1,
            busy: false,
            noMore: false,
        }
    },
    computed: {
        countOfLastPage: function () {
            return this.arr.length % this.countPerPage;

        },
        hasMore: function () {
            return this.countOfLastPage == 0 && !this.noMore;
            // return true;
        },
    },
    watch: {
        arr: {
            handler: function (n) {
                if (n.length <= this.countPerPage) {
                    this.limit_id = 1
                }
            },
            deep: true,
        }
    },
    methods: {
        loadMore: function () {
            if (!this.hasMore) {
                return;
            }
            this.busy = true;
            var data = {
                limit_id:this.arr[this.arr.length-1].id,
                member_id: getPrefs('member_id')
            };

            if (this.param) {
                concatJson(data, this.param);
            }

            var that = this;
            var callback = function (ret) {
                var more = that.retAttr ? ret.content[that.retAttr] : ret.content;
                if (more.length > 0) {
                  // console.log(JSON.stringify(more))
                    that.arr.push.apply(that.arr, more);
                } else {
                    that.noMore = true;
                }
                that.busy = false;
            };
            var errback = function () {
                that.busy = false
            };
            pinet.post(this.url, data, callback, errback);
        },
        doScroll: function () {
						var scrollTop = document.documentElement.scrollTop || window.pageYOffset || document.body.scrollTop;
            if (scrollTop + window.innerHeight >= document.body.scrollHeight - 100) {
                if (!this.busy) {
                    this.loadMore();
                }
            }
        },
    },
    mounted: function () {
        window.addEventListener('scroll', this.doScroll);
    }

});



    function toBase64(data) {
        var result = '';
        var length = data.length;
        var i;
        // Convert every three bytes to 4 ascii characters.

        for (i = 0; i < (length - 2); i += 3) {
            result += toBase64Table[data.charCodeAt(i) >> 2];
            result += toBase64Table[((data.charCodeAt(i) & 0x03) << 4) + (data.charCodeAt(i + 1) >> 4)];
            result += toBase64Table[((data.charCodeAt(i + 1) & 0x0f) << 2) + (data.charCodeAt(i + 2) >> 6)];
            result += toBase64Table[data.charCodeAt(i + 2) & 0x3f];
        }

        // Convert the remaining 1 or 2 bytes, pad out to 4 characters.

        if (length % 3) {
            i = length - (length % 3);
            result += toBase64Table[data.charCodeAt(i) >> 2];
            if ((length % 3) == 2) {
                result += toBase64Table[((data.charCodeAt(i) & 0x03) << 4) + (data.charCodeAt(i + 1) >> 4)];
                result += toBase64Table[(data.charCodeAt(i + 1) & 0x0f) << 2];
                result += base64Pad;
            } else {
                result += toBase64Table[(data.charCodeAt(i) & 0x03) << 4];
                result += base64Pad + base64Pad;
            }
        }
        return result;
    }
    //1.加密解密方法使用：

    //2.加密、解密算法封装：

    function Base64() {

        // private property
        _keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";

        // public method for encoding
        this.encode = function (input) {
            var output = "";
            var chr1, chr2, chr3, enc1, enc2, enc3, enc4;
            var i = 0;
            input = _utf8_encode(input);
            while (i < input.length) {
                chr1 = input.charCodeAt(i++);
                chr2 = input.charCodeAt(i++);
                chr3 = input.charCodeAt(i++);
                enc1 = chr1 >> 2;
                enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
                enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
                enc4 = chr3 & 63;
                if (isNaN(chr2)) {
                    enc3 = enc4 = 64;
                } else if (isNaN(chr3)) {
                    enc4 = 64;
                }
                output = output +
                _keyStr.charAt(enc1) + _keyStr.charAt(enc2) +
                _keyStr.charAt(enc3) + _keyStr.charAt(enc4);
            }
            return output;
        }

        // public method for decoding
        this.decode = function (input) {
            var output = "";
            var chr1, chr2, chr3;
            var enc1, enc2, enc3, enc4;
            var i = 0;
            input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");
            while (i < input.length) {
                enc1 = _keyStr.indexOf(input.charAt(i++));
                enc2 = _keyStr.indexOf(input.charAt(i++));
                enc3 = _keyStr.indexOf(input.charAt(i++));
                enc4 = _keyStr.indexOf(input.charAt(i++));
                chr1 = (enc1 << 2) | (enc2 >> 4);
                chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
                chr3 = ((enc3 & 3) << 6) | enc4;
                output = output + String.fromCharCode(chr1);
                if (enc3 != 64) {
                    output = output + String.fromCharCode(chr2);
                }
                if (enc4 != 64) {
                    output = output + String.fromCharCode(chr3);
                }
            }
            output = _utf8_decode(output);
            return output;
        }

        // private method for UTF-8 encoding
        _utf8_encode = function (string) {
            string = string.replace(/\r\n/g,"\n");
            var utftext = "";
            for (var n = 0; n < string.length; n++) {
                var c = string.charCodeAt(n);
                if (c < 128) {
                    utftext += String.fromCharCode(c);
                } else if((c > 127) && (c < 2048)) {
                    utftext += String.fromCharCode((c >> 6) | 192);
                    utftext += String.fromCharCode((c & 63) | 128);
                } else {
                    utftext += String.fromCharCode((c >> 12) | 224);
                    utftext += String.fromCharCode(((c >> 6) & 63) | 128);
                    utftext += String.fromCharCode((c & 63) | 128);
                }

            }
            return utftext;
        }

        // private method for UTF-8 decoding
        _utf8_decode = function (utftext) {
            var string = "";
            var i = 0;
            var c = c1 = c2 = 0;
            while ( i < utftext.length ) {
                c = utftext.charCodeAt(i);
                if (c < 128) {
                    string += String.fromCharCode(c);
                    i++;
                } else if((c > 191) && (c < 224)) {
                    c2 = utftext.charCodeAt(i+1);
                    string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
                    i += 2;
                } else {
                    c2 = utftext.charCodeAt(i+1);
                    c3 = utftext.charCodeAt(i+2);
                    string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
                    i += 3;
                }
            }
            return string;
        }
    }
